import torch

def accuracy(output, target):
    """计算准确率"""
    with torch.no_grad():
        batch_size = target.size(0)
        pred = torch.stack([out.argmax(1) for out in output], dim=1)
        correct = torch.all(pred == target, dim=1).sum().item()
        return correct / batch_size

def parse_predictions(predictions):
    """解析模型输出为字符串"""
    char_list = [str(i) for i in range(10)]
    char_list.append('')
    
    ch1, ch2, ch3, ch4 = [p.argmax(1) for p in predictions]
    ch1, ch2, ch3, ch4 = [char_list[i.item()] for i in ch1], \
                         [char_list[i.item()] for i in ch2], \
                         [char_list[i.item()] for i in ch3], \
                         [char_list[i.item()] for i in ch4]
                         
    return [''.join(chars) for chars in zip(ch1, ch2, ch3, ch4)] 